package juc.executors;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolExecutorBuild {
    public static void main(String[] args) {
        ThreadPoolExecutor executorService = (ThreadPoolExecutor) buildThreadPoolExecutor();
        int activeCount = -1;
        int queueSize = -1;
        while (true) {
            if (activeCount != executorService.getActiveCount() || queueSize != executorService.getQueue().size()) {
                System.out.println("核心线程数" + executorService.getCorePoolSize());
                System.out.println("最大线程数" + executorService.getMaximumPoolSize());
                System.out.println("活动线程数" + executorService.getActiveCount());
                System.out.println("阻塞队列数" + executorService.getQueue().size());
                activeCount = executorService.getActiveCount();
                queueSize = executorService.getQueue().size();
                System.out.println("===");
            }
        }
    }

    /**
     * Testing point.
     * <p>
     * 1.coreSize=1,MaxSize=2,blockingQueue is empty. what happen when submit 3 task?
     * 2.coreSize=1,MaxSize=2,blockingQueue size=5. what happen when submit 7 task?
     * 3.coreSize=1,MaxSize=2,blockingQueue size=5. what happen when submit 8 task?
     * <p>
     *     当提交的总数大于corePoolSize时，超出的放入BlockingQueue
     *     当提交的总数大于corePoolSize+BlockingQueue，不超过maximumPoolSize+BlockingQueue时，创建新线程
     *     当提交的总数超过maximumPoolSize+BlockingQueue时，执行RejectedExecutionHandler策略
     * <p>
     * int corePoolSize 核心线程数，即使空闲，除非设置了allowCoreThreadTimeOut为true会被回收。
     * int maximumPoolSize 池中允许的最大线程数
     * long keepAliveTime 清除比corePoolSize 多的空闲线程的最大时间
     * TimeUnit unit 参数的时间单位
     * BlockingQueue<Runnable> workQueue 保存Execute方法提交的任务到工作队列，超出corePoolSize的线程会保存到队列。队列满了则会创建一个线程
     * ThreadFactory threadFactory 线程工厂
     * RejectedExecutionHandler handler 执行策略，当线程总数超出最大线程数+队列容量被阻塞时
     * RejectedExecutionException
     */
    private static ExecutorService buildThreadPoolExecutor() {
        ExecutorService executorService = new ThreadPoolExecutor(1, 3, 7, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(2), r -> {
            Thread thread = new Thread(r);
            return thread;
        }, new ThreadPoolExecutor.AbortPolicy());

        System.out.println("the ThreadPoolExecutor create done.");
        executorService.execute(() -> timeSleep(4));
        executorService.execute(() -> timeSleep(4));
        executorService.execute(() -> timeSleep(4));
        //executorService.execute(() -> timeSleep(4));
        //executorService.execute(() -> timeSleep(4));
        //executorService.execute(() -> timeSleep(100));
        return executorService;

    }

    private static void timeSleep(long time) {
        try {
            System.out.println("*" + Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
